package com.zim.access.infrastructurre;

import com.zim.access.infrastructurre.common.AccessConfig;
import com.zim.access.infrastructurre.etcd.EtcdClientAdvanced;
import com.zim.access.infrastructurre.etcd.EtcdClientAdvancedImpl;
import com.zim.access.infrastructurre.protocol.DefaultProtocol;
import com.zim.access.infrastructurre.protocol.ProtocolBootstrap;
import com.zim.access.infrastructurre.protocol.api.Protocol;
import com.zim.access.infrastructurre.protocol.api.Provider;
import com.zim.access.infrastructurre.protocol.ws.WebsocketHandler;
import com.zim.access.infrastructurre.protocol.ws.WebsocketProtocol;
import com.zim.access.infrastructurre.protocol.ws.WebsocketProvider;
import io.etcd.jetcd.Client;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@ComponentScan
@EnableConfigurationProperties(AccessConfig.class)
public class AutoConfiguration {

    @Bean
    public ProtocolBootstrap protocolBootstrap() {
        return new ProtocolBootstrap();
    }

    @Bean
    public Provider websocketProvider() {
        return new WebsocketProvider();
    }

    @Bean
    @Primary
    public Protocol defaultProtocol() {
        return new DefaultProtocol();
    }

    @Bean
    public Protocol websocketProtocol() {
        return new WebsocketProtocol();
    }

    @Bean
    public Client client(AccessConfig config) {
        return Client.builder()
                .endpoints(config.getEtcd().getUrl())
                .build();
    }

    @Bean
    public EtcdClientAdvanced etcdClientAdvanced() {
        return new EtcdClientAdvancedImpl();
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public WebsocketHandler websocketHandler() {
        return new WebsocketHandler();
    }
}
